import unittest
import requests
import time

class UserAPI(unittest.TestCase):
    base_url = "http://182.151.48.185:8098/api/private/v1/"

    @classmethod
    def setUpClass(cls):
        cls.token = cls.login(cls)
        cls.header = {"Authorization": cls.token}

    def login(self):
        url = self.base_url + "login"
        mydata = {
            "username": "admin",
            "password": "123456"

        }
        res = requests.post(url, json=mydata)
        jasondata = res.json()
        token = jasondata["data"]["token"]
        return token

    def test_getusers(self):
        url = self.base_url + "users"
        p = {
            "pagenum": "1",
            "pagesize": "10"
        }
        res = requests.get(url, params=p, headers=self.header)
        self.assertEqual(200, res.status_code)
        self.assertIn("获取管理员列表成功", res.text)

    def test_one(self):
        url = self.base_url + "users"
        now = time.localtime()
        formattime = time.strftime("%Y%m%d%H%M%S", now)
        username = "teacherZhu"
        data = {
            "username": username,
            "password": "123456",
            "email": "12345678@qq.com"
        }
        res = requests.post(url, data=data, headers=self.header)
        self.assertEqual(200, res.status_code)
        self.assertIn("用户名已存在", res.text)

        def test_two(self, id="1002"):
            url = self.baseUrl + "users/" + id
            res = requests.get(url, headers=self.header)
            print(res.json())
            self.assertIn("获取成功", res.text)
if __name__ == "__main__":
    unittest.main()

